﻿package net.pezzato.genetree.info{
	import net.pezzato.genetree.graphics.NodeWidget;
	import net.pezzato.graphics.Widget;
	public class GeneNode {
		public static const MALE:Number = 1;
		public static const FEMALE:Number = 2;
		private var ready_:Boolean;
		private var f_:GeneNode;
		private var m_:GeneNode;
		private var c_:Array;
		private var info_:XML;
		private var photo_:XML;
		private var id_:String;
		private var widget_:NodeWidget;
		private var sex_:Number;
		private var knowsParents_;
		public function GeneNode(xml:XML = null) {
			knowsParents_ = false;
			if (xml && xml != null) {
				fromXml(xml);
			} else {
				ready_ = false;
			}
		}
		public function get id():String {
			return id_;
		}
		public function get f():GeneNode {
			return f_;
		}
		public function get m():GeneNode {
			return m_;
		}
		public function get c():Array {
			return c_;
		}
		public function get info():XML {
			return info_;
		}
		public function get photo():XML {
			return photo_;
		}
		public function get isReady():Boolean {
			return ready_;
		}
		public function get sex():Number { 
			return sex_;
		}
		public function knowsParents():Boolean {
			return knowsParents_;
		}
		private function fromXml(xml:XML):void {
			try {
				trace(xml.@id);
				var sexStr:String = xml.name().localName;
				if (sexStr == "m") {
					sex_ = MALE;
				} else if (sexStr == "f") {
					sex_ = FEMALE;
				} else {
					throw Error("errorInvalidGeneNode");
				}
				if (xml.@id == undefined) {
					xml.@id = "Unknown id";
					throw Error("errorGeneNodeWithoutId");
				}
				id_ = xml.@id;
				if (xml.f == undefined || xml.m == undefined || xml.f == null || xml.m == null) {
					knowsParents_ = false;
				} else {
					knowsParents_ = true;
				}
				if (xml.info != undefined) {
					info_ = xml.info[0];
				} else {
					info_ = null
				}
				if (xml.img != undefined && xml.img != null) {
					photo_ = xml.img[0];
				} else {
					photo_ = null
				}
				f_ = new GeneNode(xml.f[0]);
				m_ = new GeneNode(xml.m[0]);
				c_ = new Array;
				for (var i:Number = 0; i < xml.c.length(); ++i) {
					c_.push(new GeneNode(xml.c[i]));
				}
				ready_ = true;
			} catch (error:Error) {
				trace(error);
				ready_ = false;
			}
		}
	}
}